<?php
session_start();
class adminController extends Controller{
    public function admindata(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $adminModel = new adminModel();
        $userDtas = $adminModel->admins($page);
        $this->assign('users',$userDtas['user']);
        $this->assign('paging',$userDtas['page']);
        $this->assign('pagesize',$userDtas['allPage']);
        $this->assign('page',$page);
        // var_dump($user);
        $user = isset($_SESSION['user']) ? $_SESSION['user'] : null;
        $this->assign('admin',$user);
        $this->display('admin.tpl');
    }
    public function update(){
        $user = isset($_POST['user']) ? $_POST['user'] : $_SESSION['user']['user'];
        $prem = isset($_POST['perm']) ? $_POST['perm'] : $_SESSION['user']['perm'];
        $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['user']['pwd'];
        $uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['user']['uid'];

        $adminModel = new adminModel();
        $ad = $adminModel->aupdate($user,$prem,$pwd,$uid);
        if($ad){
            $this->admindata();
        }
        
    }
    public function remadmin(){
        $aid = isset($_GET['id']) ? $_GET['id'] : 0;
        $adminModel = new adminModel();
        $a = $adminModel->remadmin($aid);
        if($a){
            self::admindata();
        }else{
            
        }
    }


    public function addadmin(){
        $user = isset($_POST['user']) ? $_POST['user'] : null;
        $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : null;
        $prem = isset($_POST['perm']) ? $_POST['perm'] : null;
        $adminModel = new adminModel();
        $str = $adminModel->adduser($user,$pwd,$prem);
        // echo $str;
        if($str){
            // $thi->display('admin.tpl');
            // echo 123;
            self::admindata();
        }else{
            echo $str;
        }
        
        
    }
}